package org.tech4j.springboot.starter.edi.schedule.powerjob;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tech4j.edi.alarm.api.config.AlarmConfig;
import org.tech4j.edi.schedule.common.ScanRetryAlarmService;
import org.tech4j.edi.schedule.common.ScanRetryService;
import org.tech4j.schedule.powerjob.AlarmRetryJobHandler;
import org.tech4j.schedule.powerjob.ScanRetryJobHandler;
import org.tech4j.schedule.powerjob.SliceScanRetryJobHandler;
import org.tech4j.springboot.starter.edi.schedule.common.EdiScheduleCommonConfiguration;

/**
 * @program: tech4j
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/10 16:28
 **/
@Configuration
@AutoConfigureAfter(EdiScheduleCommonConfiguration.class)
public class EdiSchedulePowerJobConfiguration {

    @Value("${spring.profiles.active")
    private String env;

    @Bean
    @ConditionalOnBean(ScanRetryService.class)
    @ConditionalOnProperty(prefix = "tech4j.edi.schedule.powerjob", name = "execute-type", havingValue = "standalone", matchIfMissing = true)
    public ScanRetryJobHandler scanRetryJobHandler(final ScanRetryService scanRetryService) {
        return new ScanRetryJobHandler(scanRetryService);
    }

    @Bean
    @ConditionalOnBean(ScanRetryService.class)
    @ConditionalOnProperty(prefix = "tech4j.edi.schedule.powerjob", name = "execute-type", havingValue = "MapReduce")
    public SliceScanRetryJobHandler sliceScanRetryJobHandler(final ScanRetryService scanRetryService) {
        return new SliceScanRetryJobHandler(scanRetryService);
    }

    @Bean
    @ConditionalOnBean(ScanRetryAlarmService.class)
    public AlarmRetryJobHandler alarmRetryJobHandler(final ScanRetryAlarmService scanRetryAlarmService) {
        return new AlarmRetryJobHandler(env, scanRetryAlarmService);
    }
}
